<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<span id="time" style="color:forestgreen"></span>
<button id="getTime">点击获取时间</button>
</body>
</html>

<script>
    window.onload = function (){
        document.querySelector('#getTime').onclick = function (){
            //发送AJAX
            //创建XMLHttpRequest对象
            var ajax = new XMLHttpRequest();
            ajax.open('get','/getTime',true);
            ajax.onreadystatechange = function (){
                console.log(1);
                var state = ajax.readyState;
                var status = ajax.status;
                if (state ==4 && status == 200){
                    //获取数据
                    var data = ajax.responseText;
                    console.log(data);
                    var obj = JSON.parse(data);
                    console.log(obj);
                    document.querySelector('#time').innerHTML = obj.msg;

                }
            };
            //发送请求
            ajax.send();
        }
    }
</script>